<div class="layuimini-container layuimini-page-anim">


    <div class="layuimini-main">

        <blockquote class="layui-elem-quote layui-text">
            选择条件之后点击搜索，表格才会展现出来
            <br>
        </blockquote>

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">学院</label>
                            <div class="layui-input-inline">
                                <select name="college" lay-verify="" id="college-select-search" lay-filter="college-select-search">
                                    <option value="">请选择一个学院</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">专业</label>
                            <div class="layui-input-inline">
                                <select name="major" lay-verify=""  id="major-select-search" lay-filter="major-select-search">
                                    <option value="">需要先选择学院</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年级</label>
                            <div class="layui-input-inline">
                                <select name="grade" lay-verify="" id="grade-select-search" lay-filter="grade-select-search">
                                    <option value="">需要先选择专业</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">班级</label>
                            <div class="layui-input-inline">
                                <select name="class" lay-verify="" id="class-select-search" lay-filter="class-select-search">
                                    <option value="">需要先选择年级</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除所选相册 </button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId2" lay-filter="currentTableFilter2"></table>

        <script type="text/html" id="currentTableBar2">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit2">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-upload" lay-event="upload">上传资源</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="preview">查看</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        //学院选择
        jq1111.ajax({
            type: "GET",
            crossDomain:true,
            url: BasePath+"api/School/Colleges",
            contentType: "application/json",
            dataType:"json",
            xhrFields: {
                withCredentials: true
            },
            success:function (message) {
                var colleges=message.data;
                console.log(message);
                colleges.forEach(function (college) {
                    console.log(college)
                    jq1111("#college-select-search").append('' +
                        '<option value="' +
                        college.id +
                        '">' +
                        college.name +
                        '</option>' +
                        '')
                })
                //再次初始化form
                form.render();
            },
            error:function (message) {
                console.log(message.responseJSON);
                layer.msg('失败:'+message.responseJSON.message);
            },
        });

        //年纪选择
        jq1111.ajax({
            type: "GET",
            crossDomain:true,
            url: BasePath+"api/School/Grades",
            contentType: "application/json",
            dataType:"json",
            xhrFields: {
                withCredentials: true
            },
            success:function (message) {
                console.log(message)
                var grades=message.data;
                grades.forEach(function (grade) {
                    jq1111("#grade-select-search").append('' +
                        '<option value="' +
                        grade.id +
                        '">' +
                        grade.display +
                        '</option>' +
                        '')
                })
                //再次初始化form
                form.render();
            },
            error:function (message) {
                console.log(message.responseJSON);
                layer.msg('失败:'+message.responseJSON.message);
            },
        });

        //专业和班级选择
        jq1111("#major-select-search").attr("disabled","disabled");
        jq1111("#class-select-search").attr("disabled","disabled");
        jq1111("#grade-select-search").attr("disabled","disabled");

        //监听学院的select事件来获取专业
        form.on('select(college-select-search)', function(data){
            console.log(data)
            if(data.value == ""){
                jq1111("#major-select-search").attr("disabled","disabled");
                jq1111("#class-select-search").attr("disabled","disabled");

                form.render('select');
            }else{
                jq1111("#major-select-search").removeAttr("disabled");
                //移除之前添加的专业
                var children=jq1111("#major-select-search").children();
                for (i=1;i<children.length;i++){
                    children.eq(i).remove();
                }
                //获取专业
                jq1111.ajax({
                    type: "GET",
                    crossDomain:true,
                    url: BasePath+"api/School/Majors?collegeId="+data.value,
                    contentType: "application/json",
                    dataType:"json",
                    xhrFields: {
                        withCredentials: true
                    },
                    success:function (message) {
                        var majors=message.data;
                        majors.forEach(function (major) {
                            jq1111("#major-select-search").append('' +
                                '<option value="' +
                                major.id +
                                '">' +
                                major.name +
                                '</option>' +
                                '')
                        });
                        //再次初始化form
                        form.render('select');
                    },
                    error:function (message) {
                        console.log(message.responseJSON);
                    },
                });
                form.render('select');//select是固定写法 不是选择器
            }
        });

        //监听专业的select事件来设置年级
        form.on('select(major-select-search)', function(data){
            console.log(data)
            if(data.value === ""){
                jq1111('#grade-select-search').attr("disabled","disabled");
            }else{
                jq1111('#grade-select-search').removeAttr("disabled");
            }
            form.render('select');
        });

        //监听年级的select事件来设置年级
        form.on('select(grade-select-search)', function(data){
            console.log(data)
            if(data.value == ""){
                jq1111('#class-select-search').attr("disabled","disabled");
                form.render('select');
            }else{
                jq1111('#class-select-search').removeAttr("disabled");
                //移除之前添加的班级
                var children=jq1111("#class-select-search").children();
                for (i=1;i<children.length;i++){
                    children.eq(i).remove();
                }
                console.log('gradeid',jq1111('#grade-select-search'))
                //获取班级
                jq1111.ajax({
                    type: "GET",
                    crossDomain:true,
                    url: BasePath+'api/School/QueryClasses?majorId='+jq1111('#major-select-search').val()+'&gradeId='+jq1111('#grade-select-search').val(),
                    contentType: "application/json",
                    dataType:"json",
                    xhrFields: {
                        withCredentials: true
                    },
                    success:function (message) {
                        var classes=message.data;
                        classes.forEach(function (classt) {
                            jq1111("#class-select-search").append('' +
                                '<option value="' +
                                classt.id +
                                '">' +
                                classt.name +
                                '</option>' +
                                '')
                        });
                        //再次初始化form
                        form.render('select');
                    },
                    error:function (message) {
                        console.log(message.responseJSON);
                    },
                });
                form.render('select');//select是固定写法 不是选择器
            }

        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            if((data.field.major===""&&data.field.grade===""&&data.field.college==="")){
                layer.alert('参数不全，请检查检索条件', {
                    title: '参数不全，请检查检索条件'
                });
                return false;
            }
            console.log(data)
            //执行搜索重载
            table.reload('currentTableId2', {
                url: BasePath+'api/GroupAlbum/Query?'+'collegeId='+data.field.college+'&classId='+data.field.class+'&majorId='+data.field.major,
            }, 'data');

            return false;
        });

        table.render({
            elem: '#currentTableId2',
            url: BasePath+'api/GroupAlbum',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {title: '操作', width: 220, toolbar: '#currentTableBar2', align: "center"},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'title', width: 120, title: '标题', sort: true},
                {field: 'description', width: 280, title: '描述'},
                {field: 'isPublic', width: 120, title: '是否公开'},
                // {field: 'singns', title: '标签', minWidth: 150},
                // {field: 'albums', title: '相册数', minWidth: 150},
                // {field: 'views', width: 135, title: '点击量', sort: true}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        //相册删除函数
        function deleteAlbum(albumId){
            var result=false;
            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                async:false,
                url: BasePath+"api/GroupAlbum/"+albumId+"/Delete",
                contentType: "application/json",
                dataType:"json",
                data:JSON.stringify({

                }),
                xhrFields: {
                    withCredentials: true
                },
                success:function (message) {
                    result=true;
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    result=false;
                },
            });

            return result;
        }



        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter2)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/admin/group-album-manage/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '创建相册',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if(obj.event === 'delete'){ // 设置删除操作
                var checkStatus = table.checkStatus('currentTableId2')
                    , data = checkStatus.data;
                if(data.length===0){
                    layer.alert('未选择要删除的数据', {
                        title: '操作不合法'
                    });
                    return ;
                }
                var deletedItemCount=0;
                data.forEach(function (deleteData){
                    if(deleteAlbum(deleteData.id)){
                        deletedItemCount++;
                    }
                });
                //TODO 有计数bug，全部显示失败实际成功
                layer.alert('成功删除'+deletedItemCount+'个数据,'+(data.length-deletedItemCount)+'条删除失败，如果存在删除失败，请尝试刷新界面重试', {
                    title: '操作结果'
                });
                table.reload('currentTableId2',{}, 'data');
                console.log(data)
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter2)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter2)', function (obj) {
            var data = obj.data;
            //编辑相册
            if (obj.event === 'edit2') {
                var content = miniPage.getHrefContent('page/admin/group-album-manage/edit.html');
                var openWH = miniPage.getOpenWidthHeight();
                //TODO 需要一个更好的传参方法，先这样用着吧
                localStorage.setItem('selectedSchoolAlbumId',data.id);

                var index = layer.open({
                    title: '编辑相册',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'upload') {
                var content = miniPage.getHrefContent('page/admin/group-album-manage/add_recourse.html');
                var openWH = miniPage.getOpenWidthHeight();
                //TODO 需要一个更好的传参方法，先这样用着吧
                localStorage.setItem('selectedAlbumId',data.id);

                var index = layer.open({
                    title: '上传资源',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if(obj.event === 'preview'){ // 查看
                window.location = 'page/admin/group-album-manage/show.html?GroupAlbumId='+data.id;
            }
        });
        

    });
</script>